/*
========================================================================
 Name        : MMSMateContainer.cpp
 Author      : 
 Copyright   : William Yu
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <aknviewappui.h>
#include <eikappui.h>
#include <MMSMate.rsg>
// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]
#include "MMSMateContainer.h"
#include "MMSMateContainerView.h"
#include "MMSMate.hrh"
#include "MMSMateContainer.hrh"
// ]]] end generated region [Generated User Includes]

// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

#include "mmsengine.h"
#include "data.h"

/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CMMSMateContainer::CMMSMateContainer()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}
/** 
 * Destroy child controls.
 */
CMMSMateContainer::~CMMSMateContainer()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}
				
/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CMMSMateContainer
 */
CMMSMateContainer* CMMSMateContainer::NewL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CMMSMateContainer* self = CMMSMateContainer::NewLC( aRect, aParent, aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CMMSMateContainer
 */
CMMSMateContainer* CMMSMateContainer::NewLC( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CMMSMateContainer* self = new (ELeave) CMMSMateContainer();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
	}
			
/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */ 
void CMMSMateContainer::ConstructL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
    if ( !aParent )
        {
    	CreateWindowL();
        }
    else
        {
        SetContainerWindowL( *aParent );
        }
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
    InitializeControlsL();
    SetRect( aRect );
	ActivateL();
	// [[[ begin generated region: do not modify [Post-ActivateL initializations]
	// ]]] end generated region [Post-ActivateL initializations]
	
	}
			
/**
* Return the number of controls in the container (override)
* @return count
*/
TInt CMMSMateContainer::CountComponentControls() const
	{
	return (int) ELastControl;
	}
				
/**
* Get the control with the given index (override)
* @param aIndex Control index [0...n) (limited by #CountComponentControls)
* @return Pointer to control
*/
CCoeControl* CMMSMateContainer::ComponentControl( TInt aIndex ) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	switch ( aIndex )
		{
		}
	// ]]] end generated region [Generated Contents]
	
	// handle any user controls here...
	
	return NULL;
	}
				
/**
 *	Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */				
void CMMSMateContainer::SizeChanged()
	{
	CCoeControl::SizeChanged();
	LayoutControls();
	// [[[ begin generated region: do not modify [Generated Contents]
			
	// ]]] end generated region [Generated Contents]
	
	}
				
// [[[ begin generated function: do not modify
/**
 * Layout components as specified in the UI Designer
 */
void CMMSMateContainer::LayoutControls()
	{
	}
// ]]] end generated function

/**
 *	Handle key events.
 */				
TKeyResponse CMMSMateContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	
	// ]]] end generated region [Generated Contents]
	
	if ( iFocusControl &&
			iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
		return EKeyWasConsumed;
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
	}
				
// [[[ begin generated function: do not modify
/**
 *	Initialize each control upon creation.
 */				
void CMMSMateContainer::InitializeControlsL()
	{
	
	}
// ]]] end generated function

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CMMSMateContainer::HandleResourceChange( TInt aType )
	{
	CCoeControl::HandleResourceChange( aType );
	SetRect( iAvkonViewAppUi->View( TUid::Uid( EMMSMateContainerViewId ) )->ClientRect() );
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}
				
/**
 *	Draw container contents.
 */			
extern TBuf<200> gBuf;	
extern INFO gInfo;
void CMMSMateContainer::Draw( const TRect& aRect ) const
{
	// [[[ begin generated region: do not modify [Generated Contents]
	CWindowGc& gc = SystemGc();
	gc.Clear( aRect );
	
	// ]]] end generated region [Generated Contents]

	const CFont* font = CEikonEnv::Static()->DenseFont();
	gc.UseFont( font);
	
    TBidiText* bidi = TBidiText::NewL(gBuf, 10);
    bidi->WrapText(aRect.Width(), *font, NULL, 3);	
    bidi->WrapText(240, *font, NULL, 3);	
	TInt lines = bidi->NumberOfLinesInDisplayText();
	TInt baseline =	font->AscentInPixels() + 5;
	
	TRect txtrc = aRect;	
	gc.DrawText(gInfo.version, txtrc, baseline, CGraphicsContext::ELeft, 1);		
	
	txtrc.Move(0,baseline);		
    for(int i = 0; i < lines; i++)
    {
    	int width = 0;
    	TPtrC text = bidi->LineOfDisplayText(i, width);
		gc.DrawText(text, txtrc, baseline, CGraphicsContext::ELeft, 1);    	
//		gc.DrawText(gBuf, txtrc, baseline, CGraphicsContext::ELeft, 1);
    	txtrc.Move(0, baseline);
    }		
    
	delete bidi;   
	
	//
	gc.DrawText(gInfo.StartTime, txtrc, baseline, CGraphicsContext::ELeft, 1);	
	txtrc.Move(0,baseline);	

	gc.DrawText(gInfo.LatestMMSTime, txtrc, baseline, CGraphicsContext::ELeft, 1);	
	txtrc.Move(0,baseline);	

	TBuf<100> str;
	str.Format(_L("MMS sent: %d"), gInfo.MMSSent),
	gc.DrawText(str, txtrc, baseline, CGraphicsContext::ELeft, 1);	
	txtrc.Move(0,baseline);	

	str.Format(_L("Connected Times: %d"), gInfo.ConnectTimes),
	gc.DrawText(str, txtrc, baseline, CGraphicsContext::ELeft, 1);	
	txtrc.Move(0,baseline);	

	str.Copy(_L("Name of me:"));
	str.Append(gInfo.mobilename);
	gc.DrawText(str, txtrc, baseline, CGraphicsContext::ELeft, 1);	
	 					
	gc.DiscardFont();		
}
				
